package com.itheima.homework.homework04;

public class ThreadB extends Thread {
    private Lottery lottery;

    public ThreadB(Lottery lottery) {
        this.lottery = lottery;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (lottery.object) {
                if (lottery.list.size() <= 0) {
                    lottery.object.notifyAll();
                    break;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int number = lottery.r.nextInt(lottery.list.size());
                System.out.println(Thread.currentThread().getName() + "抽出奖金" + lottery.list.get(number));
                lottery.list.remove(number);
                lottery.object.notifyAll();
                try {
                    lottery.object.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
